package io.sdl.modules.sys.controller;


import io.sdl.common.constant.Constant;
import io.sdl.common.sys.dto.SdlInstitutionPicDTO;
import io.sdl.common.sys.entity.SdlInstitutionPicEntity;
import io.sdl.common.sys.service.SdlInstitutionPicService;
import io.sdl.common.utils.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("sdl/institutionPic")
public class SdlInstitutionPicController {

    @Autowired
    private SdlInstitutionPicService sdlInstitutionPicService;

    @GetMapping("page")
    @Operation(summary = "机构相关图片列表")
    @Parameters({
            @Parameter(name = Constant.PAGE, description = "当前页码，从1开始", in = ParameterIn.QUERY, required = true, ref = "int"),
            @Parameter(name = Constant.LIMIT, description = "每页显示记录数", in = ParameterIn.QUERY, required = true, ref = "int"),
            @Parameter(name = Constant.ORDER_FIELD, description = "排序字段", in = ParameterIn.QUERY, ref = "String"),
            @Parameter(name = Constant.ORDER, description = "排序方式，可选值(asc、desc)", in = ParameterIn.QUERY, ref = "String")
    })
    @RequiresPermissions("sdl:institutionPic:page")
    public Result findInstitutionPic(@Parameter(hidden = true) @RequestParam Map<String, Object> params) {
        Query query = new Query(params);
        Integer total = sdlInstitutionPicService.getNum(query);
        List<SdlInstitutionPicDTO> list = sdlInstitutionPicService.getInstitutionPicById(query);
        PageUtils pageUtil = new PageUtils(list, total, query.getLimit(), query.getPage());

        return new Result<PageUtils>().ok(pageUtil);
    }


    @PostMapping("save")
    @Operation(summary = "获取机构相关图片信息")
    @RequiresPermissions("sdl:institutionPic:save")
    public Result saveInstitutionPic(@RequestBody SdlInstitutionPicDTO sdlInstitutionPicDto) {

        String picUrl = String.join(",", sdlInstitutionPicDto.getPicList());
        sdlInstitutionPicDto.setPic(picUrl);
        SdlInstitutionPicEntity sdlInstitutionPicEntity = ConvertUtils.sourceToTarget(sdlInstitutionPicDto, SdlInstitutionPicEntity.class);
        sdlInstitutionPicService.save(sdlInstitutionPicDto);
        return new Result();
    }


}
